package com.sftz.modules.minapp.myorder.service;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.SqlManager;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sftz.assets.enums.AlbumStateEnum;
import com.sftz.assets.enums.OrderStatusEnum;
import com.sftz.assets.enums.OrderTypeEnum;
import com.sftz.modules.minapp.myorder.dto.MyOrderCountDto;
import com.sftz.modules.minapp.myorder.dto.MyOrderDto;
import com.sftz.modules.minapp.myorder.form.MyOrderForm;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.uxuexi.core.common.util.EnumUtil;
import com.uxuexi.core.common.util.MapUtil;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.dao.IDbDao;
import com.uxuexi.core.db.util.DbSqlUtil;

/**
 * 我的订单
 * @author   王宏伟
 * @Date	 2018年12月11日
 */
@IocBean
public class MyOrderService {
	private static final Log log = Logs.get();
	@Inject
	protected IDbDao dbDao;
	@Inject
	protected Dao nutDao;
	@Inject
	protected SqlManager sqlManager;

	/**
	 * 订单列表
	 * @author	王宏伟
	 * @Date	2018年12月17日
	 */
	public Map<String, Object> orderList(MyOrderForm form) {
		String token = form.getToken();
		Integer userId = RedissionUserCache.getUserId(token);
		//测试使用userId
		//		userId = 32;
		Integer pageNumber = form.getPageNumber();
		Integer pageSize = form.getPageSize();
		//获取订单状态
		Integer orderState = form.getOrderState();
		int pageNum = (pageNumber - 1) * pageSize;
		Sql sql = Sqls.create(sqlManager.get("minapp_order_list"));
		sql.setParam("userId", userId);
		sql.setParam("course", OrderTypeEnum.COURSE.intKey());
		sql.setParam("courseState", AlbumStateEnum.SUCCESS.intKey());
		sql.setParam("series", OrderTypeEnum.COURSESERIES.intKey());
		sql.setParam("status", AlbumStateEnum.AUDITED.intKey());
		sql.setParam("pageNumber", pageNum);
		sql.setParam("pageSize", pageSize);
		Cnd cnd = Cnd.NEW();
		//订单状态 
		if (!Util.isEmpty(orderState)) {
			cnd.and("a.orderState", "=", orderState);
			sql.setCondition(cnd);
		}
		cnd.and("a.amountOfMoney", "!=", 0);
		cnd.orderBy("a.orderCreateTime", "DESC");
		sql.setCondition(cnd);
		List<MyOrderDto> myOrderListPager = DbSqlUtil.query(dbDao, MyOrderDto.class, sql);
		MyOrderCountDto orderStateCount = orderStateCount(userId);
		Map<String, Object> map = Maps.newHashMap();
		map.put("myOrderList", myOrderListPager);
		map.put("pageNumber", pageNumber);
		map.put("pageSize", pageSize);
		map.put("orderStateCount", orderStateCount);
		map.put("pageNum", pageNum);
		map.put("orderState", orderState);
		return map;
	}

	public Map<String, Object> orderStateEnum() {
		//枚举- 将map转为list
		Map<String, String> map = EnumUtil.enum2(OrderStatusEnum.class);
		List<Map<String, String>> orderStateEnum = Lists.newArrayList();
		Set<String> keySet = map.keySet();
		for (String key : keySet) {
			Map<String, String> each = Maps.newHashMap();
			each.put("key", key);
			each.put("value", map.get(key));
			orderStateEnum.add(each);
		}
		//存入map
		Map<String, Object> obj = MapUtil.map();
		//订单状态枚举
		obj.put("orderStateEnum", orderStateEnum);
		return obj;
	}

	/**
	 * 获取每个订单状态的数量
	 */
	public MyOrderCountDto orderStateCount(Integer userId) {
		Sql myorderCountSql = Sqls.create(sqlManager.get("minapp_order_ordercount"));
		myorderCountSql.params().set("userId", userId);
		//WAIT_PAY(1, "等待付款"), PAYED(2, "已支付"), CANCEL(3, "已取消"), COMPLETE(4, "已完成");		myorderCountSql.params().set("userId", userId);
		myorderCountSql.params().set("wait", OrderStatusEnum.WAIT_PAY.intKey());
		myorderCountSql.params().set("complete", OrderStatusEnum.COMPLETE.intKey());
		//COURSESERIES(1, "课程系列"), COURSE(2, "课程"), WORKS(3, "作品");
		myorderCountSql.params().set("series", OrderTypeEnum.COURSESERIES.intKey());
		myorderCountSql.params().set("course", OrderTypeEnum.COURSE.intKey());
		myorderCountSql.params().set("courseState", AlbumStateEnum.SUCCESS.intKey());
		myorderCountSql.params().set("status", AlbumStateEnum.AUDITED.intKey());
		//执行单表查询  返回四个数字 用dto接
		MyOrderCountDto dto = DbSqlUtil.fetchEntity(dbDao, MyOrderCountDto.class, myorderCountSql);
		return dto;
	}
}
